﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace HobSoft.SoccerBet2014.Domain.Models
{
    using Core;
    [Serializable]
    [DataContract]
    public class Bookmaker : IModel
    {
        public Bookmaker()
        {
            Accounts = new ObservableCollection<BmAccount>();
        }

        private byte _bmId;
        [PrimaryKey]
        [Key]
        [Required]
        [DataMember(IsRequired = true)]
        public byte BmID
        {
            get { return _bmId; }
            set { _bmId = value; }
        }

        private string _bmName;
        [Required]
        [DataMember(IsRequired = true)]
        [MaxLength(50)]
        [MinLength(1)]
        public string BmName
        {
            get { return _bmName; }
            set { _bmName = value; }
        }

        private string _defaultHost;
        [Required]
        [MaxLength(200)]
        [MinLength(1)]
        [DataMember(IsRequired = true)]
        public string DefaultHost
        {
            get { return _defaultHost; }
            set { _defaultHost = value; }
        }

        private bool _available;
        [DataMember]
        public bool Available
        {
            get { return _available; }
            set { _available = value; }
        }

        private ObservableCollection<BmAccount> _accounts;
        [IgnoreDataMember]
        public ObservableCollection<BmAccount> Accounts
        {
            get { return _accounts; }
            set
            {
                if (_accounts == value) return;
                _accounts = value;
                if (_accounts != null)
                {
                    _accounts.CollectionChanged += Accounts_CollectionChanged;
                    foreach (var item in _accounts)
                    {
                        item.Booker = this;
                    }
                }
            }
        }

        private void Accounts_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace)
            {
                foreach (BmAccount item in e.NewItems)
                {
                    if (item.Booker != this)
                        item.Booker = this;
                }
                if (e.OldItems != null)
                {
                    foreach (BmAccount item in e.OldItems)
                    {
                        item.Booker = null;
                    }
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                if (e.OldItems != null)
                {
                    foreach (BmAccount item in e.OldItems)
                    {
                        item.Booker = null;
                    }
                }
            }
        }

    }
}
